package net.lunjin.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.lunjin.bean.News;
import net.lunjin.bean.NewsRemark;
import net.lunjin.service.NewsRemarkService;
import net.lunjin.service.NewsService;
import net.lunjin.util.Constants;
import net.lunjin.util.ProjectUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import finaler.util.DateUtil;
import finaler.util.Page;
import finaler.util.PageList;

@Controller
@RequestMapping("bbs")
public class BbsController {
	
	@Autowired
	private NewsService newsService;

	@Autowired
	private NewsRemarkService newsRemarkService;
	
	/**
	 * 贴子列表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/index/" )
	public String list(HttpServletRequest request) throws Exception {
		String bbsType = request.getParameter("bbsType")==null?"":request.getParameter("bbsType");
		request.setAttribute("bbsType", bbsType);
		Page page = new Page(request);
		page.setRange(20);
		page.putParameter("bbsType", request.getParameter("bbsType"));
		page.putParameter("searchkey", request.getParameter("searchkey"));
		PageList bbsList = newsService.getBbsList(page);//贴子查询
		request.setAttribute("bbsList", bbsList);

		List<News> fmNewsList = newsService.getRandomRecord(0, 5);//首页随机5条负面新闻
		request.setAttribute("fmNewsList", fmNewsList);
		List<News> knowNewsList = newsService.getTopRecord(1, 5);//5条最新投资知识
		request.setAttribute("knowNewsList", knowNewsList);
		
		return "/bbs/list";
	}

	/**
	 * 贴子详细
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/topic{topicId}.html" )
	public String detail(@PathVariable BigDecimal topicId,
			HttpServletRequest request) throws Exception {
		News topicDetail = newsService.findByKey(topicId);//新闻查询
		if(topicDetail==null){
			return "redirect:"+Constants.URL; 
		}
		request.setAttribute("topicDetail", topicDetail);
		
		News updateTopic = new News();
		updateTopic.setId(topicId);
		updateTopic.setHit(topicDetail.getHit( ).add(new BigDecimal(1)));
		newsService.update(updateTopic);
		
		List<News> fmNewsList = newsService.getRandomRecord(0, 5);//首页随机5条负面新闻
		request.setAttribute("fmNewsList", fmNewsList);
		List<News> knowNewsList = newsService.getTopRecord(1, 5);//5条最新投资知识
		request.setAttribute("knowNewsList", knowNewsList);
		
		Page page = new Page(request);
		page.setRange(10);
		page.putParameter("newsId", topicId);
		PageList topicRemarkList = newsRemarkService.getNewsRemarkList(page);
		request.setAttribute("topicRemarkList", topicRemarkList);
		
		int a = new Random().nextInt(10);
		int b = new Random().nextInt(10);
		int c = a  + b;
		request.setAttribute("bs", a+"+"+b);
		request.getSession().setAttribute("bc", c);
		
		return "/bbs/topicDetail";
	}

	/**
	 * 发表贴子
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/post.html" )
	public String post(HttpServletRequest request) throws Exception {
		int a = new Random().nextInt(10);
		int b = new Random().nextInt(10);
		int c = a  + b;
		request.setAttribute("bs", a+"+"+b);
		request.getSession().setAttribute("bc", c);
		
		return "/bbs/post";
	}

	/**
	 * 贴子提交
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/save",method=RequestMethod.POST )
	@ResponseBody
	public void save(HttpServletRequest request,HttpServletResponse response) throws Exception {
		if(request.getSession().getAttribute("bc")==null ||
				!request.getSession().getAttribute("bc").toString().equals(request.getParameter("bc"))){//防外部提交
			response.setContentType("text/html; charset=utf-8");
			response.getWriter().print("<script>alert('验证码不正确!请重新输入!');window.history.go(-1);</script>");
			return;
		}
		News bbs =  new  News();
		bbs.setNewstitle(request.getParameter("newstitle"));
		bbs.setNewscontent(request.getParameter("newscontent").replaceAll("\n", "<br/>"));
		bbs.setNewstype(new BigDecimal( request.getParameter("newstype")));
		bbs.setCreateuser(request.getRemoteAddr());
		bbs.setCreatetime(ProjectUtil.getCurrentFormatStr("yyyy-MM-dd HH:mm:ss"));
		newsService.insert(bbs);
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().print("<script>alert('发表成功!');window.location='"+Constants.URL+"/bbs/index/?bbsType="+request.getParameter("newstype")+"'</script>");
	}
	
	/**
	 * 贴子提交,给远程的自动添加软件使用
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/saveForSoft",method=RequestMethod.POST )
	@ResponseBody
	public String saveForSoft(HttpServletRequest request,HttpServletResponse response) throws Exception {
		News bbs =  new  News();
		bbs.setNewstitle(request.getParameter("newstitle"));
		bbs.setNewscontent(request.getParameter("newscontent").replaceAll("\n", "<br/>"));
		bbs.setNewstype(new BigDecimal( request.getParameter("newstype")));
		bbs.setCreateuser(ProjectUtil.getRandomIP());
		bbs.setHit(new BigDecimal(new Random().nextInt(300)));
		bbs.setBaiduid(request.getParameter("baiduid"));
		bbs.setCreatetime(ProjectUtil.getCurrentFormatStr("yyyy-MM-dd HH:mm:ss"));
		newsService.insert(bbs);
		News justNews = newsService.getByBaiduId(request.getParameter("baiduid"));
		return justNews==null?"":justNews.getId().toString();
	}
	
	@RequestMapping( value="/remark",method=RequestMethod.POST )
	public String remark(HttpServletRequest request,HttpServletResponse response) throws Exception {
		if(request.getSession().getAttribute("bc")==null ||
				!request.getSession().getAttribute("bc").toString().equals(request.getParameter("bc"))){//防外部提交
			response.setContentType("text/html; charset=utf-8");
			response.getWriter().print("<script>alert('验证码不正确!请重新输入!');window.history.go(-1);</script>");
			return null;
		}
		NewsRemark newsRemark = new NewsRemark();
		newsRemark.setRmcontent(request.getParameter("rmcontent").replaceAll("\n", "<br/>"));//内容
		newsRemark.setAutoflag(new BigDecimal(0));//正常发
		newsRemark.setNewsid(new BigDecimal(request.getParameter("topicId")));
		newsRemark.setCreatetime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
		newsRemark.setCreateuser(request.getParameter("createuser"));
		newsRemark.setAutoflag(new BigDecimal(request.getParameter("autoflag")));
		newsRemarkService.insert(newsRemark);
		return "redirect:/bbs/topic"+request.getParameter("topicId")+".html#remarkDiv"; 
	}

	@RequestMapping( value="/remarkForSoft",method=RequestMethod.POST )
	public String remarkForSoft(HttpServletRequest request,HttpServletResponse response) throws Exception {
		NewsRemark newsRemark = new NewsRemark();
		newsRemark.setRmcontent(request.getParameter("rmcontent").replaceAll("\n", "<br/>"));//内容
		newsRemark.setAutoflag(new BigDecimal(0));//正常发
		newsRemark.setNewsid(new BigDecimal(request.getParameter("topicId")));
		newsRemark.setCreatetime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
		newsRemark.setCreateuser(request.getParameter("createuser"));
		newsRemark.setAutoflag(new BigDecimal(request.getParameter("autoflag")));
		newsRemarkService.insert(newsRemark);
		return "redirect:/bbs/topic"+request.getParameter("topicId")+".html#remarkDiv"; 
	}
	
}
